// 递归
function a(obj, targetObj) {
  let cloneObj = targetObj || {}
  if (!obj || typeof obj !== 'object' || obj.length === undefined) {
    return obj
  }
  if (obj instanceof Array) {
    cloneObj = obj.concat()
  } else {
    for (let i in obj) {
      if (typeof obj[i] === 'object') {
        cloneObj[i] === a(obj[i], {})
      } else {
        cloneObj[i] = obj[i]
      }
    }
  }
  return cloneObj
}

let Obj = {
  
  a: 1,
  b: {
    a: 1
  },
  c: {
    a: 1,
    b: {
      a: 1
    }
  },
  d: function () {
    console.log('hello world')
  },
  e: [1, 2, 3]
}
let newObj = a(Obj, {})

console.log(Obj.c.b.a,newObj.c.b.a = 10);

// console.log(newObj,Obj );



/**
    深拷贝
 */

    function deepCopy(obj) {
      if(typeof obj === 'object' && obj !== null) {
          let newObj = Array.isArray(obj) ? [] : {}
          for(let key in obj) {
              newObj[key] = deepCopy(obj[key])
          } 
          return newObj
      }else {
          return obj
      }
  }
  
  let obj = {
      name: 'zhangsan',
      age: 18,
      aa: {
          a1: 11,
          a2: 22,
      },
      add: [1,2,4],
      test: null
  }
  let  newObj2 = deepCopy(obj)
  obj.aa.a1 = 22
  console.log(obj,newObj2);